#!/bin/bash

# 设置API基础URL
BASE_URL="http://localhost:3001/api"

# 注册管理员用户
echo "注册管理员用户..."
curl -X POST $BASE_URL/users/register \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "admin123",
    "role": "admin"
  }'

echo -e "\n\n登录获取token..."
# 登录获取token
TOKEN=$(curl -s -X POST $BASE_URL/users/login \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "admin123"
  }' | jq -r '.token')

echo "获取到的token: $TOKEN"

# 创建区域
echo -e "\n\n创建区域..."
curl -X POST $BASE_URL/regions \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "华东区",
    "code": "HD001",
    "level": 1
  }'

# 获取区域列表
echo -e "\n\n获取区域列表..."
curl -X GET $BASE_URL/regions \
  -H "Authorization: Bearer $TOKEN"

# 获取单个区域
echo -e "\n\n获取单个区域..."
curl -X GET $BASE_URL/regions/1 \
  -H "Authorization: Bearer $TOKEN"

# 更新区域
echo -e "\n\n更新区域..."
curl -X PUT $BASE_URL/regions/1 \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "华东大区",
    "code": "HD001",
    "level": 1
  }'

# 删除区域
echo -e "\n\n删除区域..."
curl -X DELETE $BASE_URL/regions/1 \
  -H "Authorization: Bearer $TOKEN" 